<?php
/**
 * 视觉中国自动爬虫相关
 * @author zengjie
 *
 */
class Auto_task extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		
		$this->config->load('mongodb');
		$mongo_config = $this->config->item('shijue_master');

		$this->load->library('Mongo_db', $mongo_config, 'shijue_master');
		
		$this->shijue_master->switch_db("auto");
	}
	
	function index($offset = 0){
		$company_id = isset($_GET['company_id']) ? $_GET['company_id'] : 0;
		$task_id = isset($_GET['task_id']) ? $_GET['task_id'] : 0;
		$clue_id = isset($_GET['clue_id']) ? $_GET['clue_id'] : 0;
		$sort = isset($_GET['sort']) ? $_GET['sort'] : 1;

		$where = array();
		if($company_id > 0){
			#$where['company_id'] = $company_id;
		}

		if($task_id > 0){
			#$where['task_id'] = $task_id;
		}

		if($clue_id > 0){
			#$where['clue_id'] = $clue_id;
		}

        $c = array();
        
        if($clue_id > 0){
            $data = $this->shijue_master
            ->select(array("img_url","width","height","url","pic_size","status"))
            ->where( $where )
            ->order_by(array("pic_size"=>$sort))
            ->offset( $offset )
            ->limit(1000)
            ->get("clue_id_".$clue_id);

            $count = $this->shijue_master
                ->where( $where )
                ->count("clue_id_".$clue_id);

            $c = array("lists"=>$data, "map"=>$_GET);
            $c['clue'] = 'clue';
            $c['count'] = $count;
            $c['username'] = $this->session->userdata('username');

            $this->load->library('pagination');
            $config = array(
                'base_url' => site_url().'/auto_task/index',
                'total_rows' => $count,
                'per_page' => 1000,
                'first_tag_open' => '<li>',
                'first_tag_cloase' => '</li>',
                'next_tag_open' => '<li>',
                'next_tag_close' => '</li>',
                'prev_tag_open' => '<li>',
                'prev_tag_close' => '</li>',
                'cur_tag_open' => '<li class="active"><a onclick="javascript:return false;">',
                'cur_tag_close' => '</a ></li>',
                'num_tag_open' => '<li>',
                'num_tag_close' => '</li>',
                'last_tag_open' => '<li>',
                'last_tag_close' => '</li>',
                'first_link' => '首页',
                'last_link' => '尾页',
                'reuse_query_string' => true,
            );
            $this->pagination->initialize($config);
        }

		$this->load->view('auto', $c);
	}

	//提交clue_id 至队列，submit_task监听并提交数据库具体数据
	function submit() {
        $clue_id = $this->input->post("clue_id");

        $this->load->model('queue_model') ;

        $data = array('clue_id' => $clue_id);
        $result = $this->queue_model->add_cid_task($data);

        if ($result) {
            $this->json_return('',200,'success');
        }
        $this->json_return('',0,'error');
    }

    function delete() {
        $post = $this->input->post("vehicle");
        $clue_id = $this->input->post("clue_id");
        $res = 0;
        foreach($post as $key => $val) {
            $id = new MongoId($val);
            $result = $this->shijue_master
                ->where(array('_id' => $id))
                ->update("clue_id_".$clue_id, array('status' => "0"));
            if ($result) {
                $res++;
            }
        }

        if ($res) {
            $this->json_return('',200,'success');
        }
        $this->json_return('',0,'error');
    }

    function rollback() {
        $post = $this->input->post("vehicle");
        $clue_id = $this->input->post("clue_id");

        $res = 0;
        foreach($post as $key => $val) {
            $id = new MongoId($val);
            $result = $this->shijue_master
                ->where(array('_id' => $id))
                ->update("clue_id_".$clue_id, array('status' => "1"));
            if ($result) {
                $res++;
            }
        }

        if ($res) {
            $this->json_return('',200,'success');
        }
        $this->json_return('',0,'error');

    }

    //监听clue_id，提交数据库具体数据，get_task监听并作具体处理
    function submit_task() {
        $this->load->model('queue_model') ;

        $param = $this->queue_model->get_cid_task();

        if (empty($param)) {
            echo "sleep 3 \n";
            sleep(3);
            return false;
        }

        $clue_id = $param["clue_id"];

        $data = $this->shijue_master
            #->select(array("img_url","width","height","url","pic_size","status"))
            ->get_where("clue_id_".$clue_id, array("status" => "1"), 1000);

        $this->load->model('queue_model') ;

        foreach ($data as $k => $v) {
            $task_result = $this->queue_model->add_task($v);

            $result = $this->shijue_master
                ->where(array('_id' => $v['_id']))
                ->update("clue_id_".$clue_id, array('status' => "2"));
        }

        $len = count($data);
        #echo "submit count :" . $len . "\n";exit;
        if ($len == 1000) {
            $data = array('clue_id' => $clue_id);
            $result = $this->queue_model->add_cid_task($data);
            if ($result) {
                echo "more data to deal , send task, clue_id : ". $clue_id ."\n";
            } else {
                echo "send task error, clue_id : ". $clue_id ."\n";
            }
        }

        echo "submit count :" . $len . "\n";
    }

    //监听autospider_img_url 数据，处理
    function get_task($num = 1) {
        $this->load->model('queue_model') ;

        while($num <= 50){
            $num ++;
            $param = $this->queue_model->get_task();

            if (empty($param)) {
                echo "sleep 3 \n";
                sleep(3);
                return false;
            }

            echo $param['img_url'];

            $data = array();
            $data["pid"] = time() . mt_rand (10000,99999);
            $data["task_id"] = $param['task_id'];
            $data["clue_id"] = $param['clue_id'];
            $data["clue_name"] = $param['clue_name'];
            $data["company_id"] = $param['company_id'];
            $data["url"] = $param['url'];
            $data["pic_url"] = $param['img_url'];
            $data["client_date"] = "";
            $data["url_article_title"] = $param['title'];
            $data["url_article"] = $param['content'];
            $data["is_cover"] = 0;

            $post = array("data"=>json_encode(array("resource"=>$data)));
            #print_r($post );exit;
            $rs = $this->send_http("http://shijue.qingapi.cn/task_python/start", $post);

            echo "#########" . $rs . "\n";
        }
    }

    function json_return($data, $code, $msg) {
        $json['data'] = $data ;
        $json['code'] = $code ;
        $json['msg'] = $msg ;

        echo json_encode($json);
        die;
    }

    function send_http($url, $post = array(), $timeout = 25, $header = array(), $connecttimeout = 5) {

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macsh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connecttimeout);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

        if($post){
            if(is_array($post)){
                $post = http_build_query($post);
            }

            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        }

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
        
        $rs = curl_exec($ch);
        $http_info = curl_getinfo($ch);
        
        curl_close($ch);
        if($http_info['http_code'] != 200 && $http_info['http_code'] != 302 ){
            return false;
        }
        return $rs;
    }


}